package com.cskaoyan.utils;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建日期: 2022/10/10 17:19
 *
 * @author ciggar
 */
public class DBCPUtils {

    static DataSource dataSource;


    static {

//        BasicDataSource basicDataSource = new BasicDataSource();

//        basicDataSource.setUsername();
//        basicDataSource.setUrl();
//        basicDataSource.setPassword();
//        basicDataSource.setConnectionProperties();
//
//
//        basicDataSource.setInitialSize();
//        basicDataSource.setMaxIdle();
//        basicDataSource.setMinIdle();
//        basicDataSource.setMaxWait();

        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("dbcp.properties"));

            dataSource = BasicDataSourceFactory.createDataSource(properties);

        }catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    // 获取DBCP数据库连接池创建的连接
    public static Connection getConnection(){

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }
}
